package app.sceledroid.activity.course;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import app.sceledroid.R;
import app.sceledroid.activity.MainActivity;
import app.sceledroid.activity.course.forum.DiscussionActivity.DiscussionActivity;
import app.sceledroid.adapter.ForumAdapter;
import app.sceledroid.adapter.ForumAdapter;
import app.sceledroid.entity.Forum;
import app.sceledroid.entity.Forum;
import app.sceledroid.services.SceleService;
import app.sceledroid.services.SceleService.SceleBinder;

public class ForumActivity extends Activity {


	private SceleService sceleService;
	private boolean isBounded;
	private ArrayList<Forum> forums;
	private ForumAdapter forumAdapter;
	private String courseName;
	private ServiceConnection sceleServiceConnection = new ServiceConnection() {

		//@Override
		public void onServiceConnected(ComponentName componentName,
				IBinder iBinder) {
			// TODO Auto-generated method stub
			SceleBinder binder = (SceleBinder) iBinder;
			sceleService = binder.getService();
			isBounded = true;
		}

		//@Override
		public void onServiceDisconnected(ComponentName componentName) {
			// TODO Auto-generated method stub
			isBounded = false;
		}

	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.course_forum_view);
		
		forums = new ArrayList<Forum>();
		forumAdapter = new ForumAdapter(ForumActivity.this,
				R.id.list_no_item, forums);
		Intent service = new Intent(this, SceleService.class);
		startService(service);

		Bundle bundle = getIntent().getExtras();
		courseName = bundle.getString("CourseName");
		
		final Button fetchButton = (Button) findViewById(R.id.fetchForum);
		ListView forumListView = (ListView) findViewById(R.id.forumList);
		
		forumListView.setOnItemClickListener(new OnItemClickListener() {


			public void onItemClick(AdapterView<?> adapterView, View view,
					int index, long position) {
				// TODO Auto-generated method stub
				// Intent intent = new Intent(DeadlineActivity.this,
				// CourseActivity.class);

				Forum r = forumAdapter.getItem(index);

				Toast.makeText(ForumActivity.this, r.getName() + r.getUrl(),
						Toast.LENGTH_LONG);
				if (sceleService != null) {
					Intent intent = new Intent(ForumActivity.this,
							DiscussionActivity.class);

					Forum f = forumAdapter.getItem(index);
					Log.v("Selected", "Selected Forum: " + f.getName());
					
					if (sceleService.getDiscussion(f.getName()) == null)
					{
						sceleService.fetchDiscussion(ForumActivity.this, f);
						
					}
					else if (sceleService.getDiscussion(f.getName()).size() == 0) {
						Toast.makeText(ForumActivity.this,
								"Deadline not fetched", Toast.LENGTH_LONG)
								.show();
						sceleService.fetchDiscussion(ForumActivity.this, f);
					}
					
					//intent.putExtra("URL", f.getUrl());
					intent.putExtra("ForumName", f.getName());
					
					//intent = new Intent(ForumActivity.this, MainActivity.class);
					getParent().startActivity(intent);
				}
				else
				{
					bindToService();
					Toast.makeText(ForumActivity.this, "SceleService IS NULL", Toast.LENGTH_LONG).show();
				}
			}
		});		
		forumListView.setAdapter(forumAdapter);

		fetchButton.setOnClickListener(new OnClickListener() {

			//@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(MainActivity.sceleService != null)
				{
					Toast.makeText(ForumActivity.this, "Scele Service Not null", Toast.LENGTH_LONG);
				}
				
				if (sceleService != null) {
					Toast.makeText(ForumActivity.this,
							"Scele Service Not NULL", Toast.LENGTH_LONG).show();

					ArrayList<Forum> temp = sceleService.getForum(courseName);
					Log.v("Course Name : ", courseName );
					if (temp != null) {
						forums.addAll(temp);
						for (Forum r : forums) {
							Log.v("Forum : ", r.getName() + r.getUrl());
						}
						forumAdapter.notifyDataSetChanged();
						// listView.

						fetchButton.setVisibility(View.GONE);
					}
					else
					{
						Toast.makeText(ForumActivity.this,
								"Deadlines is NULL", Toast.LENGTH_LONG).show();
					}
				} else {
					
					Toast.makeText(ForumActivity.this, "Scele Service NULL",
							Toast.LENGTH_LONG).show();
					bindToService();
				}
			}

		
		});
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		bindToService();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	private void bindToService() {
		Intent service = new Intent(this, SceleService.class);
		getParent().bindService(service, sceleServiceConnection, Context.BIND_AUTO_CREATE);

	}

}
